package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ResumeInfo;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.ResumeInfoService;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 简历信息
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ResumeInfoController {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private ResumeInfoService resumeInfoService;

    /**
     * 新增简历
     * @param resumeInfo
     * @return
     */
    @PostMapping("/resumes")
    public AjaxResult insert(@Valid @RequestBody ResumeInfo resumeInfo){
        log.info("Rest request to insert resumeInfo:{}",resumeInfo);
        SysUser user = userUtils.getCurrentUser();
        resumeInfo.setUserId(user.getId());
        Integer id = resumeInfoService.insert(resumeInfo);
        ResumeInfo result = resumeInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新简历
     * @param resumeInfo
     * @return
     */
    @PutMapping("/resumes")
    public AjaxResult update(@Valid @RequestBody ResumeInfo resumeInfo){
        log.info("Rest request to update resumeInfo:{}",resumeInfo);
        resumeInfoService.update(resumeInfo);
        ResumeInfo result = resumeInfoService.selectById(resumeInfo.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询简历详情
     * @param id
     * @return
     */
    @GetMapping("/resumes/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find resumeInfo by id:{}",id);
        ResumeInfo result = resumeInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除简历
     * @param id
     * @return
     */
    @DeleteMapping("/resumes/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete resumeInfo by id:{}",id);
        int row = resumeInfoService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取当前用户简历
     * @param pageable
     * @return
     */
    @GetMapping("/resumes-mine")
    public AjaxResult selectByMine(Pageable pageable){
        log.info("Rest request to find res by resumeInfo");
        SysUser user = userUtils.getCurrentUser();
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ResumeInfo> resumeInfos = resumeInfoService.selectByParams(null,null,null,user.getId(),null);
        PageInfo<ResumeInfo> result = new PageInfo<>(resumeInfos);
        return AjaxResult.success(result);
    }

    /**
     * 分页查询简历
     * @param keyword 关键字
     * @param residence 居住地
     * @param wantedJob 求职意向
     * @param workYear 工作年份
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/resumes")
    public AjaxResult selectByParams(@RequestParam(required = false,defaultValue = "") String keyword,@RequestParam(required = false,defaultValue = "") String residence,@RequestParam(required = false ) Integer wantedJob,@RequestParam(required = false ) Integer workYear, Pageable pageable){
        log.info("Rest request to find res by resumeInfo by keyword:{},residence:{},wantedJob:{},workYear:{}",keyword,residence,wantedJob,workYear);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ResumeInfo> resumeInfos = resumeInfoService.selectByParams(keyword,residence,wantedJob, null, workYear);
        PageInfo<ResumeInfo> result = new PageInfo<>(resumeInfos);
        return AjaxResult.success(result);
    }
}
